package top.houry.im.server.handler.impl;

import com.google.protobuf.TextFormat;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.houry.im.server.listener.ImServerNettyProcessorListener;
import top.houry.im.server.proto.ImMessage;
import top.houry.im.server.handler.ImServerMsgTypeHandler;

@Slf4j
@Service(value = "client.heartbeat.req")
public class ImServerHeartBeatMsgHandlerImpl implements ImServerMsgTypeHandler {

    @Override
    public void processImMsg(ImMessage.Server server, ChannelHandlerContext ctx) {
        try {
            ImMessage.HeartBeatReqMessage reqMessage = ImMessage.HeartBeatReqMessage.parseFrom(server.getBytesData());
            log.info("[Req]-[ImServerHeartBeatMsgHandlerImpl]-[processImMsg]-[reqMessage:{}]", TextFormat.printToUnicodeString(reqMessage));
            ImMessage.HeartBeatRespMessage.Builder respMessage = ImMessage.HeartBeatRespMessage.newBuilder();
            ctx.writeAndFlush(respMessage).addListener(new ImServerNettyProcessorListener("心跳回复"));
        } catch (Exception e) {
            log.error("[Exception]-[ImServerHeartBeatMsgServiceImpl]-[processImMsg]", e);
        }
    }
}
